import 'package:flutter/material.dart';

import '../../../config/global_config.dart';
import '../bean/login_info.dart';

class IosTextField extends StatelessWidget {
  Map info;
  static bool _isChange = false;

  // 用户名、密码输入框的控制器
  static TextEditingController account = TextEditingController();
  static TextEditingController password = TextEditingController();

  static final _accountFocus = FocusNode();
  static final _passwordFocus = FocusNode();

  IosTextField(Map info) : this.info = info;

  @override
  Widget build(BuildContext context) {
    String accountValue = _isChange
        ? account.text
        : info["account"].isEmpty
            ? ""
            : info["account"];
    String passwordValue = _isChange
        ? password.text
        : info["password"].isEmpty
            ? ""
            : info["password"];

    // 获取account值
    account.value = _TextEditingValue(accountValue);
    password.value = _TextEditingValue(passwordValue);

    // 创建widget
    return Container(
      width: double.infinity,
      child: Column(
        children: [
          TextField(
            controller: account,
            focusNode: _accountFocus,
            keyboardType: TextInputType.text,
            textInputAction: TextInputAction.done,
            keyboardAppearance: Brightness.light,
            cursorColor: GlobalConfig.theme,
            cursorWidth: 1,
            decoration: InputDecoration(
              hintText: '请输入账户',
              filled: true,
              fillColor: Color.fromRGBO(255, 255, 255, .8),
              hintStyle: TextStyle(color: GlobalConfig.assistColor),
              prefixIcon: Icon(Icons.account_box),
              enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: GlobalConfig.borderColor),
              ),
              focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: GlobalConfig.theme),
              ),
            ),
            onChanged: (String v) {
              if (!_isChange) _isChange = true;
            },
          ),
          TextField(
            controller: password,
            focusNode: _passwordFocus,
            keyboardType: TextInputType.text,
            textInputAction: TextInputAction.done,
            keyboardAppearance: Brightness.light,
            cursorColor: GlobalConfig.theme,
            cursorWidth: 1,
            obscureText: true,
            decoration: InputDecoration(
              hintText: '请输入密码',
              filled: true,
              fillColor: Color.fromRGBO(255, 255, 255, .8),
              hintStyle: TextStyle(color: GlobalConfig.assistColor),
              prefixIcon: Icon(Icons.keyboard),
              enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: GlobalConfig.borderColor),
              ),
              focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: GlobalConfig.theme),
              ),
            ),
            onChanged: (String v) {
              if (!_isChange) _isChange = true;
            },
          ),
        ],
      ),
    );
  }

  /// 为控制器创建value（此做法防止光标移位）
  /// * [editvalue] String|需要编辑的值|必填
  TextEditingValue _TextEditingValue(String editvalue) {
    return TextEditingValue(
      text: editvalue,
      selection: TextSelection.fromPosition(
        TextPosition(
          affinity: TextAffinity.downstream,
          offset: editvalue.length,
        ),
      ),
    );
  }

  /// 验证结果
  static LoginInfoBean verificaResult() {
    int len = 5;
    String _account = account.text.trim(), _pwd = password.text.trim();

    if (_account.isEmpty) {
      return LoginInfoBean(msg: '账户不能为空');
    }

    if (_account.length < 5) {
      return LoginInfoBean(msg: '账户不可小于5位');
    }

    if (_pwd.isEmpty) {
      return LoginInfoBean(msg: '密码不能为空');
    }

    if (_pwd.length < 5) {
      return LoginInfoBean(msg: '密码不可小于5位');
    }

    return LoginInfoBean.fromJson({
      "status": true,
      "data": {"account": account.text, "password": password.text},
      "msg": "通过",
    });
  }
}
